package com.leimo.config.web_socket2;

import com.leimo.web_socket2.interceptor.CustomHandshakeInterceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author 段誉
 * @create 2019-02-26 14:35
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
  @Autowired
  private WebSocketHandler webSocketHandler;

  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    // 注册自定义消息处理，消息路径为 /websocket
    registry.addHandler(webSocketHandler, "/web_socket")
            //配置跨域
            .setAllowedOrigins("*")
            //配置拦截器
            .addInterceptors(new CustomHandshakeInterceptor());
  }
}
